<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible.sync="visible"
    width="60%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="150px">
      <el-form-item label="id" prop="id"  hidden>
        <el-input v-model="dataForm.id" placeholder="id" hidden ></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="物理库" prop="databaseId">
            <el-select
              v-model.trim="dataForm.databaseId"
              filterable
              @change="selectByDatabaseIds($event)"
              placeholder="请选择物理库"
              style="width:100%"
            >
              <el-option
                v-for="database in databaseOptions"
                :key="database.id"
                :label="database.database.databaseName+'_'+database.schemaNameCn"
                :value="database.id"
              ></el-option>
            </el-select>
          </el-form-item>

        </el-col>
        <el-col :span="12">
          <el-form-item label="资料名称" prop="ddataClassId">

            <el-select
              v-model.trim="dataForm.ddataClassId"
              filterable
              @change="getTable($event)"
              placeholder="请选择资料"
              style="width:100%"
            >
              <el-option
                v-for="dataClass in dataClassIdOptions"
                :key="dataClass.DATA_CLASS_ID"
                :label="dataClass.CLASS_NAME"
                :value="dataClass.DATA_CLASS_ID"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
       <!-- <el-col :span="12">
          <el-form-item label="资料名称" prop="dataName">
            <el-input v-model="dataForm.dataName" placeholder="资料名称"></el-input>
          </el-form-item>
        </el-col>-->
        <el-col :span="12">
          <el-form-item label="表名" prop="tableName">
            <el-input v-model="dataForm.tableName" placeholder="表名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="四级编码" prop="ddataId">
            <el-input v-model="dataForm.ddataId" placeholder="四级编码"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="及时时间" prop="timely">
            <el-input v-model="dataForm.timely" placeholder="请输入及时时间"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最小应收数" prop="minFileNum">
            <el-input v-model="dataForm.minFileNum" placeholder="最小应收数"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="资料时次" prop="jobCron">
            <el-input v-model="dataForm.jobCron" placeholder="资料时次"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否应收一致" prop="isFit">
            <el-radio-group v-model="dataForm.isFit">
              <el-radio  :label=0>一致</el-radio>
              <el-radio  :label=1>不一致</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>
<style>
  .tip {
    padding: 8px 16px;
    background-color: #ecf8ff;
    border-radius: 4px;
    border-left: 5px solid #50bfff;
    margin: 20px 0;
  }
</style>
<script>
  import {getObj, addObj, putObj,findAllDataBase,getByDatabaseId,getByDatabaseIdAndClassId} from '@/api/job/playBill'

  export default {
    data() {
      return {
        visible: false,
        canSubmit: false,
        databaseOptions: [],
        dataClassIdOptions:[],
        dataForm: {
          id:'',
          databaseId:'',
          ddataId: '',
          ddataClassId: '',
          dataName: '',
          timely : '',
          minFileNum: '',
          isFit: 0,
          jobCron:'',
          triggerStatus: 1,
        },
        dataRule: {

        }
      }
    },
    created() {
      this.getDataBase();
    },
    methods: {
      init(id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (id) {
            getObj(id).then(response => {
              this.dataForm = response.data.data;
              this.selectByDatabaseIds(this.dataForm.databaseId)
            })
          }
        })
      },
      getDataBase(){
        findAllDataBase().then(response => {
          this.databaseOptions = this.unique(response.data.data);
        })
      },
      unique(arr) {
        const res = new Map();
        return arr.filter((arr) => !res.has(arr.database.id) && res.set(arr.database.id, 1)&&arr.database.id=='FIDB')
      },
      selectByDatabaseIds(databaseId) {
         getByDatabaseId(databaseId).then(response => {
          this.dataClassIdOptions = response.data.data;
        });
      },
      getTable(dataClassId) {
        this.dataForm.dataName=this.changeLocationValue(dataClassId);
        getByDatabaseIdAndClassId(this.dataForm.databaseId, dataClassId).then(response => {
          console.log(response)
          this.dataForm.ddataId = response.data.data.ddataId;
          this.dataForm.tableName = response.data.data.tableName;
        });
      },
      changeLocationValue(val){
        //locations是v-for里面的也是datas里面的值
        let obj = {};
        obj = this.dataClassIdOptions.find((item)=>{
          return item.DATA_CLASS_ID === val;
        });
        let name = ''
        name = obj.CLASS_NAME;
        return name
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$notify.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
        this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>

